{% extends "base.html" %}

{% block title %}规则配置列表 - 规则配置管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="bi bi-list-ul"></i> 规则配置列表</h2>
            <a href="/web/rule-configs/create" class="btn btn-primary">
                <i class="bi bi-plus-circle"></i> 新建配置
            </a>
        </div>

        {% if configs %}
        <!-- 搜索框 -->
        <div class="mb-4">
            <div class="input-group">
                <span class="input-group-text"><i class="bi bi-search"></i></span>
                <input type="text" class="form-control" id="configSearch" 
                       placeholder="搜索配置名称、业务类型或描述...">
            </div>
        </div>
        
        <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4" id="configList">
            {% for config in configs %}
            <div class="col config-item" data-name="{{ config.name }}" 
                 data-type="{{ config.business_type }}" 
                 data-desc="{{ config.description or '' }}">
                <div class="card h-100 card-hover">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <h5 class="card-title">
                                <a href="/web/rule-configs/{{ config.id }}" class="text-decoration-none">
                                    {{ config.name }}
                                </a>
                            </h5>
                            {% if config.enabled %}
                            <span class="badge bg-success">启用</span>
                            {% else %}
                            <span class="badge bg-secondary">禁用</span>
                            {% endif %}
                        </div>
                        
                        <p class="card-text">
                            <span class="badge badge-business-type bg-primary">
                                {{ config.business_type }}
                            </span>
                        </p>
                        
                        {% if config.description %}
                        <p class="card-text text-muted small">{{ config.description }}</p>
                        {% endif %}
                        
                        <div class="mt-3 d-flex justify-content-between text-muted small">
                            <div>
                                <i class="bi bi-file-earmark-text"></i> {{ config.extraction_rules|length }} 个提取规则
                            </div>
                            <div>
                                <i class="bi bi-check-circle"></i> {{ config.audit_rules|length }} 个审核规则
                            </div>
                        </div>
                        
                        <div class="mt-3 text-muted small">
                            <i class="bi bi-clock"></i> 
                            更新于: {{ config.updated_at.strftime('%Y-%m-%d %H:%M') if config.updated_at else config.created_at.strftime('%Y-%m-%d %H:%M') }}
                        </div>
                    </div>
                    <div class="card-footer bg-transparent">
                        <div class="d-flex justify-content-between">
                            <a href="/web/rule-configs/{{ config.id }}" class="btn btn-sm btn-outline-primary">
                                <i class="bi bi-eye"></i> 查看
                            </a>
                            <form method="post" action="/web/rule-configs/{{ config.id }}/delete" 
                                  onsubmit="return confirm('确定要删除此配置吗？');" style="display: inline;">
                                <button type="submit" class="btn btn-sm btn-outline-danger">
                                    <i class="bi bi-trash"></i> 删除
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
        {% else %}
        <div class="alert alert-info text-center" role="alert">
            <i class="bi bi-info-circle"></i> 暂无规则配置，
            <a href="/web/rule-configs/create" class="alert-link">点击这里创建第一个配置</a>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    // 配置搜索功能
    const configSearch = document.getElementById('configSearch');
    if (configSearch) {
        configSearch.addEventListener('input', function(e) {
            const searchText = e.target.value.toLowerCase();
            const items = document.querySelectorAll('.config-item');
            let visibleCount = 0;
            
            items.forEach(item => {
                const name = item.dataset.name.toLowerCase();
                const type = item.dataset.type.toLowerCase();
                const desc = item.dataset.desc.toLowerCase();
                
                if (name.includes(searchText) || type.includes(searchText) || desc.includes(searchText)) {
                    item.style.display = '';
                    visibleCount++;
                } else {
                    item.style.display = 'none';
                }
            });
            
            // 如果没有匹配的结果，显示提示
            const configList = document.getElementById('configList');
            let noResultDiv = document.getElementById('noSearchResult');
            
            if (visibleCount === 0 && searchText) {
                if (!noResultDiv) {
                    noResultDiv = document.createElement('div');
                    noResultDiv.id = 'noSearchResult';
                    noResultDiv.className = 'col-12';
                    noResultDiv.innerHTML = '<div class="alert alert-warning"><i class="bi bi-exclamation-triangle"></i> 没有找到匹配的配置</div>';
                    configList.appendChild(noResultDiv);
                }
            } else {
                if (noResultDiv) {
                    noResultDiv.remove();
                }
            }
        });
    }
</script>
{% endblock %}

